class MytodoController < ApplicationController
  layout "mytodo"  
  
  before_filter :authorize
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
  :redirect_to => { :controller=>:dashboard, :action => :index }  
  
  def show
    @todo = Todo.find(params[:id])
  end
  
  def new
    @todo = Todo.new 
    @todo.is_full_day = 1
    @todo.check_state = 0
    @todo.repeat_days = 0
    @todo.create_for_date = session[:date] if session[:date]
    @todo.end_time = Time.now + 30.minutes
    @back = params[:back]
  end
  
  def create
    @todo = Todo.new(params[:todo])
    if @todo.save
      flash[:notice] = 'Todo was successfully created.'      
      if @todo.back         
        redirect_to :controller => @todo.back.to_s,  :action => 'index'
      else
        redirect_to :action => 'list'
      end
    else
      render :action => 'new'
    end
  end
  
  def edit
    @todo = Todo.find(params[:id])          
  end
  
  def update
    @todo = Todo.find(params[:id])
    if @todo.update_attributes(params[:todo])
      flash[:notice] = 'Todo was successfully updated.'
      if @todo.back 
        redirect_to :controller => @todo.back.to_s,  :action => 'index'
      else
        redirect_to :action => 'show', :id => @todo
      end 
    else
      render :action => 'edit'
    end
  end
  
  def continue
    old = Todo.find(params[:id])
    @todo = old.continue
    redirect_to :action => 'edit', :id => @todo, :back=>params[:back]     
  end
  
  def timer    
    @todo = Todo.find(params[:id])
  end
  
  def note    
    @todo = Todo.find(params[:id])
    @note = Note.new
    @note.todo = @todo      
  end
  
  def note_create
    @note = Note.new(params[:note])
    @note.update_time = Time.now
    if @note.save
      flash[:notice] = 'Note was successfully created.'                     
      redirect_to :controller => "dashboard",  :action => 'index'     
    else
      render :action => 'note'
    end
  end
  
  def destroy
    Todo.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
end
